<template>
	<view class="page-wrap" :data-theme="theme" :class="theme" :style="globalColor.style">
		<HeadCustom ref="HeadCustom" title="加入企业" :isBack="false" linkId="usrus195jn6ptrd" :linkData="linkData"></HeadCustom>
		<view class="wrap fboxCol Ycenter">
			<template v-if="fromData.state == 0">
			<view class="info fboxRow Ycenter bg-21242C w750">
				<image class="pic img_bg" :src="detail.companyLogo"></image>
				<view class="fboxCol">
					<view class="name font38 font-ell1 lh44">{{detail.companyName}}</view>
					<view class="mt8 color-6B707C font26 lh38">来自 <text class="color-E8CDA7">{{fromData.realName}}</text> 的邀请二维码</view>
				</view>
			</view>
			<view class="bd w750">
				<view class="color-E8CDA7 font30 lh44">完善企业信息</view>
				<view class="tips mt10 font26 color-6B707C"><text class="font30">温馨提示：</text>加入企业，需要完善职位信息</view>
				<view class="content bg-21242C mt40 border-radius12">
					<view class="item fboxRow Ycenter bg-1A1D25 border-radius8">{{detail.companyName}}</view>
					<view class="item mt24 fboxRow Ycenter bg-1A1D25 border-radius8" @click="goPosition">
						<view class="flex1">
							<text v-if="fromData.companyPosition">{{fromData.companyPosition}}</text>
							<text class="color-6B707C" v-else>请选择职位</text>
						</view>
						<u-icon name="arrow-right" color="#6B707C"></u-icon>
					</view>
				</view>
			</view>
			<view class="flex1"></view>
			<view class="btn fboxRow Ycenter Xcenter bg-linear font32 color-141720" @click="submit">下一步</view>
			</template>
			<template v-else-if="fromData.state == 2">
				<view class="fboxCol Ycenter Xcenter ptb40 wait">
					<image class="pic" src="https://ebc-prod.cdn.benwunet.com/uni-static/img3.3.0/wait.png" mode=""></image>
					<view class="tit font30 lh44 mt32">等待审批</view>
					<view class="tips color-6B707C font26 mt16"><text class="color-E8CDA7">{{fromData.realName}}</text>邀请你加入企业，等待审批</view>
				</view>
				<view class="flex1"></view>
				<view class="btn fboxRow Ycenter Xcenter bg-linear font32 color-141720" @click="back">返回首页</view>
			</template>
		</view>
		<not-login position="center" ref="notLogin" />
	</view>
</template>

<script>
	import { companyCardPreview, joinCompanyCheck, joinCompany } from '@/api/card.js';
	import {base64decode, utf8to16, base64encode} from '@/static/js/Base64.js';
	export default {
		data() {
			return {
				theme: getApp().globalData.appTheme,
						globalColor: getApp().globalData.globalColor,
				detail: {},
				fromData:{
					realName:'',
					companyId:'',
					companyName:'',
					companyPosition: '',
					companyLogo: '',
					inviteesId: "",
					invitorId: "",
					state: 0
				},
				linkData:{
					id: '',
					realName: '',
					invitorId: ''
				},
				id: '',
				isLogin: false
			}
		},
		onLoad(options) {
			if(options && options.id){
				this.id = options.id
				this.fromData.companyId = options.companyId
			}
		},
		onShow() {
			this.getInit()
			this.getCompany()
			const filterTemp = getApp().globalData.filterTemp
			if(filterTemp.companyPosition){
				this.fromData.companyPosition = filterTemp.companyPosition
			}
			const {bw_link, bw_params} = getApp().getBWData(['bw_link', 'bw_params'])
			if(bw_params.access_token){
				this.isLogin = true
				this.getState()
			}else{
				this.isLogin = false
				// this.$nextTick(() => {
				// 	this.$refs.notLogin.openLogin({
				// 		show: true,
				// 		position: 'center'
				// 	})
				// })
			}
		},
		methods: {
			getInit(){
				const {bw_link} = getApp().getBWData(['bw_link'])
				const bwlink = decodeURIComponent(bw_link.linkSource)
				const data = JSON.parse(utf8to16(base64decode(bwlink)))
				this.fromData.realName = data.realName
				this.fromData.memberId = data.memberId
				this.fromData.invitorId = data.memberId
				this.fromData.companyId = data.companyId
				this.fromData.id = data.companyId
				this.linkData = {
					realName: data.realName,
					invitorId: data.memberId,
					id: data.companyId
				}
			},
			getState(){
				const {bw_user_temp} = getApp().getBWData(['bw_user_temp'])
				this.fromData.inviteesId = bw_user_temp.memberId
				this.linkData.inviteesId = bw_user_temp.memberId
				joinCompanyCheck({
					companyId: this.fromData.companyId,
					inviteesId: bw_user_temp.memberId
				}).then(res=>{
					if(res.data == 0){
						this.getCompany()
					}else if(res.data == 2){
						this.fromData.state = 2
					}else{
						uni.showToast({
							icon: 'none',
							title:'该用户已加入企业，无需重复申请'
						})
						setTimeout(()=>{
							this.$pageTo('/index/index')
						},1000)
					}
				})
			},
			getCompany() {
				companyCardPreview({companyId: this.id}).then(res=>{
					this.detail = res.data
					this.fromData.companyId =  res.data.companyId
					this.fromData.companyName = res.data.companyName
					this.fromData.companyLogo = res.data.companyLogo
				})
			},
			submit(){
				if(this.isLogin){
					joinCompany(this.fromData).then(res=>{
						this.fromData.state = 2
					})
				}else{
					this.$nextTick(() => {
						this.$refs.notLogin.openLogin({
							show: true,
							position: 'center'
						})
					})
				}
			},
			goPosition(){
				if(this.isLogin){
					getApp().globalData.filterTemp = {companyPosition: ''}
					this.$pageTo('/recruit/common/position?isCheckbox=0&pageType=joinComany')
				}else{
					this.$nextTick(() => {
						this.$refs.notLogin.openLogin({
							show: true,
							position: 'center'
						})
					})
				}
			},
			back(){
				this.$pageTo('/index/index')
			}
		}
	}
</script>

<style lang="scss" scoped>
	.wrap{min-height: calc(84vh);}
	.w750{width: 750rpx;}
	.info{padding: 40rpx;}
	.info .pic{ margin-right: 36rpx;width: 88rpx;height: 88rpx;background: #D8D8D8;border-radius: 12rpx}
	.bd{padding: 40rpx;}
	.content{padding: 40rpx;}
	.content .item{height: 90rpx;padding: 0 32rpx;}
	.btn{ width: 570rpx;height: 76rpx;box-shadow: 0rpx 8rpx 32rpx 0rpx rgba(0,0,0,0.16);border-radius: 12rpx}
	.wait{padding: 128rpx 40rpx 0;}
	.wait .pic{width: 120rpx;height: 120rpx}
	
</style>